package cn.yh.register.hosp.service.impl;

import cn.yh.register.hosp.repository.DepartmentRepository;
import cn.yh.register.hosp.service.DepartmentService;
import cn.yh.register.model.hosp.Department;
import cn.yh.register.vo.hosp.DepartmentQueryVo;
import cn.yh.register.vo.hosp.DepartmentVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentRepository departmentRepository;


    /**
     * 保存科室
     * @param objectMap 科室信息
     */
    @Override
    public void saveDepartment(Map<String, Object> objectMap) {
        //把数据保存到mongoDB中 先转成JSON数据
        String jsonString = JSONObject.toJSONString(objectMap);
        //再把JSON转成对象
        Department department = JSONObject.parseObject(jsonString, Department.class);
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();
        //看mongoDB中是否有此数据 有就修改 没有就添加
        Department dept = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (dept != null) {
            //修改
            department.setId(dept.getId());
            department.setCreateTime(dept.getCreateTime());
            department.setIsDeleted(dept.getIsDeleted());

        } else {
            //新增
            department.setCreateTime(new Date());
            department.setIsDeleted(0);
        }
        department.setUpdateTime(new Date());
        departmentRepository.save(department);
    }

    /**
     * 分页查询科室列表
     * @param page    当前页
     * @param size    每页条数
     * @param queryVo 查询条件
     * @return 分页数据
     */
    @Override
    public Page<Department> findByPage(Integer page, Integer size, DepartmentQueryVo queryVo) {
        Department department = new Department();
        //Copy查询条件
        BeanUtils.copyProperties(queryVo, department);
        department.setIsDeleted(0);
        //构造条件对象 Example
        Example<Department> example = Example.of(department, ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true));
        //构造sort排序对象
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        //构造Pageable对象 页码从零开始
        PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
        //分页查询
        Page<Department> departmentPage = departmentRepository.findAll(example, pageRequest);
        return departmentPage;
    }

    /**
     * 删除科室
     * @param hoscode 医院编号
     * @param depcode 科室编号
     */
    @Override
    public void removeDept(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.delete(department);
        }
    }

    /**
     * 树形展示科室列表
     * @param hoscode 医院编号
     * @return 科室列表数据
     */
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //查询所有科室
        List<Department> departmentList = departmentRepository.findByHoscode(hoscode);
        //按照大科室分组  转为map  key 表示大科室编号 value 表示该大科室所有的小科室
        Map<String, List<Department>> depMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历Map 处理数据
        List<DepartmentVo> voList = new ArrayList<>();
        for (Map.Entry<String, List<Department>> entry : depMap.entrySet()) {
            //key 表示大科室编号
            String bigcode = entry.getKey();
            // value 表示该大科室所有的小科室
            List<Department> deptList = entry.getValue();
            //组装大科室
            DepartmentVo bigVo = new DepartmentVo();
            bigVo.setDepcode(bigcode);
            bigVo.setDepname(deptList.get(0).getDepname());//大科室名称

            //组装小科室
            List<DepartmentVo> children=new ArrayList<>();
            for (Department dept : deptList) {
                DepartmentVo smallVo = new DepartmentVo();
                smallVo.setDepcode(dept.getDepcode());
                smallVo.setDepname(dept.getDepname());
                children.add(smallVo);
            }
            //将小科室装入大科室
            bigVo.setChildren(children);
            //将大科室放入集合

            voList.add(bigVo);
        }
        return voList;
    }
}
